"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**基础数据处理 */
const m_base_1 = tslib_1.__importDefault(require("../utils/m-base"));
/**文件目录处理 */
const m_file_1 = tslib_1.__importDefault(require("../utils/m-file"));
/**配置文件处理 */
const m_config_1 = tslib_1.__importDefault(require("../utils/m-config"));
/**命令处理 */
class MCmder {
    /**解析参数列表 */
    static decodeArgs(args) {
        const argNames = {
            addr: "-a,addr".split(","), //请求地址
            dir: "-d,dir".split(","), //生成目录
            name: "-n,name".split(","), //项目名称
            user: "-u,user".split(","), //授权用户名
            offline: "-o,offline".split(","), //使用线下文件
            pass: "-p,pass".split(","), //授权密码
            writer: "-w,writer".split(","), //书写器名称
        };
        const parms = args instanceof Array ? args : args.split(/\s+/);
        const result = {};
        for (const p in parms) {
            if (typeof parms[p] != "string")
                continue;
            if (!parms[p].includes("="))
                continue;
            const [key, val] = parms[p].split("=").map((x) => m_base_1.default.trimStr(x));
            for (let n in argNames) {
                if (argNames[n].includes(key))
                    result[n] = val || "";
            }
        }
        return result;
    }
    /**逆向解析参数 */
    static encodeArgs(parm) {
        const { cmdName, apiName, dir, addr, offline } = parm;
        const { authUser, authPass, writerName } = parm;
        const res = {};
        let cmd = `npm run ${cmdName}`;
        res["-a"] = addr;
        res["-d"] = dir;
        res["-n"] = apiName;
        res["-u"] = authUser;
        res["-p"] = authPass;
        res["-o"] = offline ? "true" : "";
        res["-w"] = writerName;
        cmd = `${cmd} -n=${apiName}`;
        cmd = `${cmd} -d=${dir}`;
        cmd = `${cmd} -w=${writerName}`;
        if (offline)
            cmd = `${cmd} -o=true`;
        if (authUser)
            cmd = `${cmd} -u=${authUser} -p=${authPass}`;
        cmd = `${cmd} -a=${addr}`;
        res["cmd"] = cmd;
        return res;
    }
    /**获取命令信息 */
    static async getParm() {
        const infos = process.argv || [];
        const res = {};
        const [service, script, ...args] = infos;
        const other = MCmder.decodeArgs(args);
        const { addr, dir, name, user, pass, offline, writer } = other;
        const config = await m_config_1.default.load();
        const cmdPath = m_file_1.default.getRoot();
        const cmdName = config.getName() || "";
        const url = addr || "";
        const dirPath = dir || "./src/api";
        const apiName = name || "test";
        const workPath = m_base_1.default.trimStr(cmdPath.split(/[/\\]node_modules[/\\]/i)[0]);
        const isOffline = (offline || "false").toLowerCase() == "true";
        res.service = service;
        res.script = script;
        res.addr = url;
        res.dir = dirPath;
        res.authUser = user || "";
        res.authPass = pass || "";
        res.offline = isOffline;
        res.cmdName = cmdName;
        res.cmdPath = cmdPath;
        res.workPath = workPath;
        res.apiName = apiName;
        res.writerName = writer || "ts+axios";
        res.apiPath = m_file_1.default.joinDirPath(workPath, dirPath, apiName);
        res.swgFile = isOffline ? MCmder.getClientUrl(res, url || "./") : "";
        res.swgUrl = isOffline ? "" : MCmder.getServerUrl(url);
        res.swgJson = { info: { title: "", version: "" } };
        res.result = { methods: [], models: [], enums: [] };
        return res;
    }
    /**获取本地配置文件地址 */
    static getClientUrl(parm, path) {
        const paths = [parm.workPath, path];
        if (!path.endsWith(".json"))
            paths.push("swagger.json");
        return m_file_1.default.joinDirPath(...paths);
    }
    /**获取远程配置文件地址 */
    static getServerUrl(host) {
        const isUrl = /^https?:\/\//i.test(host) || false;
        const url = isUrl ? host : `http://${host}`;
        const urls = [url];
        if (!host)
            return "";
        if (!url.endsWith(".json"))
            urls.push("swagger.json");
        return m_file_1.default.joinUrlPath(...urls);
    }
}
exports.default = MCmder;
